package com.lbj.googleplay.googleplay100.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.View;

import com.lbj.googleplay.googleplay100.global.MyApplication;

public class UIUtils {
    //获取全局context对象
    public static Context getContext(){
        return MyApplication.getContext();
    }
    //获取全局Handler对象
    public static Handler getHandler(){
        return MyApplication.getHandler();
    }

    //获取全局主线程id
    public static long getMainThreadId(){
        return MyApplication.getMainThreadId();
    }

    //获取strings.xml中的某个字符串
    public static String getString(int id){

        return getContext().getResources().getString(id);
    }

    //获取strings.xml中的某个字符串数组
    public static String[] getStringArray(int id){

        return getContext().getResources().getStringArray(id);
    }
    //获取图片
    public static Drawable getDrawable(int id){

        return getContext().getResources().getDrawable(id);
    }

    //获取颜色
    public static int getColor(int id){

        return getContext().getResources().getColor(id);
    }
    //根据id获取颜色的状态选择器
    public static ColorStateList getColorStateList(int id) {

        return getContext().getResources().getColorStateList(id);
    }
    //获取尺寸
    public static int getDimen(int id){
        //返回具体的像素值
        return getContext().getResources().getDimensionPixelSize(id);
    }

    // dp转px
    public static int dip2px(float dip){

        float density = getContext().getResources().getDisplayMetrics().density;

        return (int) (dip*density+0.5f);
    }
    // px转dp
    public static float px2dip(float px){

        float density = getContext().getResources().getDisplayMetrics().density;

        return  px/density;
    }

    // 加载布局文件
    public static View inflate(int id){

        return  View.inflate(getContext(),id,null);
    }
    //判断是否在主线程
    public static boolean isRunOnUIThread(){
        //获取当前线程id
        long id = Thread.currentThread().getId();
        //与主线程id进行比较
        return getMainThreadId() == id;
    }
     //
    public static void runOnUIThread(Runnable r){

        if (isRunOnUIThread()){
            //如果当前已经是主线程,则直接run
            r.run();
        }else {
            //如果是子线程,借助Handler的post让其运行在主线程
            getHandler().post(r);
        }
    }


}
